﻿#ifndef FD_DATASET_LOCK_MANAGER_H_
#define FD_DATASET_LOCK_MANAGER_H_

#include "udatabase/dll_cfg.h"
#include "udatabase/sqldatabase.h"
#include "kernel/Referenced.h"

namespace FD
{
	namespace Utility
	{
		namespace DB
		{
			namespace Interface
			{
				class IFileDataSource;
			}
		}
	}
}

namespace FD
{
	namespace Utility
	{
		namespace DB
		{
			// <summary>
			// 数据集锁管理类
			// 提供数据集锁查询，锁，破锁功能
			// </summary>
			class CDatasetLockInfo;
			typedef std::vector<FD::Utility::DB::CDatasetLockInfo> DatasetLockInfoVector;

			class FD_DB_Export CDatasetLockManager : public FD::Kernel::CReferenced
			{
			public:
				/// <summary>
				/// 析构函数
				/// </summary>
				/// <returns>bool，TRUE成功/FALSE失败</returns>
				virtual ~CDatasetLockManager();

				/// <summary>
				/// 获取单例
				/// </summary>
				/// <returns>bool，TRUE成功/FALSE失败</returns>
				static FD::Utility::DB::CDatasetLockManager* getSingleton();

				/// <summary>
				/// 析构单例
				/// </summary>
				/// <returns>bool，TRUE成功/FALSE失败</returns>
				static void releaseSingleton();

				/// <summary>
				/// 给数据集加锁
				/// </summary>
				/// <returns>bool，TRUE成功/FALSE失败</returns>
				bool lockDataset(FD::Utility::DB::CSqlDatabase& db, const String& datasetname);
				bool lockDataset(FD::Utility::DB::Interface::IFileDataSource* pDataSource, const String& datasetName);
				/// <summary>
				/// 给数据集解锁
				/// </summary>
				/// <returns>bool，TRUE成功/FALSE失败</returns>					
				bool unlockDataset(FD::Utility::DB::CSqlDatabase& db, const String& datasetname);
				bool unlockDataset(FD::Utility::DB::CSqlDatabase& db);
				bool unlockDataset(FD::Utility::DB::Interface::IFileDataSource* pDataSource, const String& datasetName);
				/// <summary>
				/// 强制解锁
				/// </summary>
				/// <param name=""></param>
				/// <returns></returns>

				bool breakDatasetLock(FD::Utility::DB::CSqlDatabase& db, const String& datasetname);

				/// <summary>
				/// 检查数据集是否被锁
				/// </summary>
				/// <returns>bool，TRUE成功/FALSE失败</returns>
				bool isDatasetLocked(FD::Utility::DB::CSqlDatabase& db, const String& datasetname);
				bool isDatasetLocked(FD::Utility::DB::Interface::IFileDataSource* pDataSource, const String& datasetname);
				/// <summary>
				/// 判断数据集是否已经被自己锁定
				/// </summary>
				/// <param name=""></param>
				/// <returns></returns>

				bool isDatasetLockedBySelf(FD::Utility::DB::CSqlDatabase& db, const String& datasetname);
				bool isDatasetLockedBySelf(FD::Utility::DB::Interface::IFileDataSource* pDataSource, const String& datasetname);
				/// <summary>
				/// 查询所有数据集锁状态
				/// </summary>
				/// <returns>bool，TRUE成功/FALSE失败</returns>
				FD::Utility::DB::DatasetLockInfoVector queryAllLockInfo(FD::Utility::DB::CSqlDatabase& db);

				/// <summary>
				/// 
				/// </summary>
				/// <param name=""></param>
				/// <returns></returns>

				const String& lockerName() const;

			private:
				CDatasetLockManager();

				static FD::Utility::DB::CDatasetLockManager* mpSingleton;

				String mGuid;

				/// <summary>
				/// 
				/// </summary>
				/// <param name=""></param>
				/// <returns></returns>

				static INT32 getDatasetID(FD::Utility::DB::CSqlDatabase& db, const String& datasetname);
			};

			class FD_DB_Export CDatasetLockInfo : public FD::Kernel::CReferenced
			{
			public:
				CDatasetLockInfo();

				String mDatasetName;
				String mGuid;
				String mTypeName;
				INT32 mJASID;
				INT32 mType;
			};
		}
	}
}
#endif
